package com.runa.rccprotocol.dto.novocon;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.extern.slf4j.Slf4j;

import static com.runa.rccprotocol.dto.novocon.CJT188Struct.CtrlCode.READ;
import static com.runa.rccprotocol.dto.novocon.CJT188Struct.CtrlCode.SETTING;
import static com.runa.rccprotocol.dto.novocon.CJT188Struct.DeviceType.*;

@Slf4j
public class NovoconRequestData extends ConcentratorRequestData {

    private CJT188Struct struct;

    public NovoconRequestData(String address) {
        super(address);
    }

    public void readHeatMeter(String meter) {
        struct = new CJT188Struct(HEAT_METER, meter, READ);
        struct.setParamField(0x901f, new byte[]{0x12});
        setHaveResult(true);
    }

    public void readHeatMeterSdt(String meter) {
        struct = new CJT188Struct(HEAT_METER_SDT, meter, READ);
        struct.setParamField(0x1f90, new byte[]{3});
        setHaveResult(true);
    }

    public void readBeacon(String valve) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, READ);
        struct.setParamField(0x1010, new byte[]{0});
        setHaveResult(true);
    }

    public void readTempCtrl(String panel) {
        struct = new CJT188Struct(TEMP_CTRL, panel, READ);
        struct.setParamField(0x901f, new byte[]{0});
        setHaveResult(true);
    }

    public void readTempCtrlOfSpark(String panel) {
        struct = new CJT188Struct(TEMP_CTRL_SPARK, panel, READ);
        struct.setParamField(0x0035, new byte[]{0});
        setHaveResult(true);
    }

    public void beaconSetting(String valve, int caliber) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, SETTING);
        byte RI = 0;
        switch (caliber) {
            case 20:
                RI = 3;
                break;
            case 25:
                RI = 4;
                break;
            case 32:
                RI = 5;
                break;
        }
        struct.setParamField(0x0100, new byte[]{0, RI});
        setHaveResult(true);
    }

    public void beaconStrokeCtrl(String valve) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, SETTING);
        struct.setParamField(0x0f00, new byte[]{0, 0});
        setHaveResult(true);
    }

    public void beaconPosition(String valve, float opening) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, SETTING);
        int integer = (int) opening;
        int fractional = (int) ((opening - integer) * 100);
        struct.setParamField(0x0900, new byte[]{0, (byte) fractional, (byte) integer});
        setHaveResult(true);
    }

    public void beaconTempCtrl(String valve) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, SETTING);
        struct.setParamField(0x0f00, new byte[]{0, 1});
        setHaveResult(true);
    }

    public void beaconTemperature(String valve, float temp) {
        struct = new CJT188Struct(HOUSE_VALVE, valve, SETTING);
        int integer = (int) temp;
        int fractional = (int) ((temp - integer) * 100);
        struct.setParamField(0x1000, new byte[]{0, (byte) fractional, (byte) integer});
        setHaveResult(true);
    }

    public void tempCtrlOpen(String panel, boolean model, float opening) {
        struct = new CJT188Struct(TEMP_CTRL, panel, SETTING);
        struct.setParamField(0xa017, new byte[]{1, 1, 4, (byte) (model ? 1 : 0), 1, 0, (byte) opening});
        setHaveResult(true);
    }

    public void tempCtrlValveLock(String panel, boolean lock) {
        struct = new CJT188Struct(TEMP_CTRL, panel, SETTING);
        struct.setParamField(0xa017, new byte[]{1, 2, 1, (byte) (lock ? 1 : 0)});
        setHaveResult(true);
    }

    public void tempCtrlTempLock(String panel, boolean lock, float temp) {
        struct = new CJT188Struct(TEMP_CTRL, panel, SETTING);
        int integer = Integer.parseInt(String.valueOf((int) temp), 16);
        int fractional = Integer.parseInt(String.valueOf((int) ((temp* 100 - Math.floor(temp)* 100))), 16);
        struct.setParamField(0xa017, new byte[]{1, 8, 3, (byte) (lock?1:0), (byte) fractional, (byte) integer});
        setHaveResult(true);
    }

    public static void main(String[] args) {
        float temp= (float) 23.9;
        System.out.println((Math.floor(temp)));
        System.out.println(((temp)));
        System.out.println((temp* 100 - Math.floor(temp)* 100));
        System.out.println(String.valueOf((int) ((temp - Math.floor(temp)) * 100)));
        System.out.println(Integer.parseInt(String.valueOf((int) ((temp* 100 - Math.floor(temp)* 100))), 16));
    }

    public void tempCtrlTempLimit(String panel, float up, float down) {
        struct = new CJT188Struct(TEMP_CTRL, panel, SETTING);
        int upInteger = (int) up;
        int upFractional = (int) ((up - upInteger) * 100);
        int downInteger = (int) down;
        int downFractional = (int) ((down - downInteger) * 100);
        byte[] bs = new byte[7];
        bs[0] = 1;
        bs[1] = 9;
        bs[2] = 4;
        bs[3] = (byte) Integer.parseInt(String.valueOf(downFractional), 16);
        bs[4] = (byte) Integer.parseInt(String.valueOf(downInteger), 16);
        bs[5] = (byte) Integer.parseInt(String.valueOf(upFractional), 16);
        bs[6] = (byte) Integer.parseInt(String.valueOf(upInteger), 16);
        struct.setParamField(0xa017, bs);
        setHaveResult(true);
    }

//    @Override
//    public boolean check(IResponseData data) {
//        return true;
//    }

    @Override
    public byte[] getData() {
        return struct.getBuffer();
    }

//    public static void main(String[] args) {
//        NovoconRequestData data = newComm NovoconRequestData("00000058");
//        data.readHeatMeter("03400020");
//        System.out.println("读热表 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.readBeacon("20686299741234");
//        System.out.println("读阀门 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.readTempCtrl("10023861");
//        System.out.println("读温控器"+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.beaconStrokeCtrl("20686299741234");
//        System.out.println("设置阀门按百分比控制 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.beaconPosition("20686299741234", 50);
//        System.out.println("阀门开度"+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.beaconTempCtrl("20686299741234");
//        System.out.println("阀门按温度控制 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.beaconTemperature("20686299741234", (float) 25.6);
//        System.out.println("阀门温度设置 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.tempCtrlOpen("10016671", false, 59);
//        System.out.println("温控器设置开度 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.tempCtrlValveLock("10023392", true);
//        System.out.println("温控器开度锁 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.tempCtrlTempLock("10016671", false, (float) 25.5);
//        System.out.println("阀门温度锁 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//        data.tempCtrlTempLimit("10016671", 30, 22);
//        System.out.println("阀门上下限 "+ByteUtil.byteToHexString((byte[]) data.getData()));
//
//        data.readHeatMeter("61005610");
//        System.out.println(ByteUtil.byteToHexString(data.getData()));
//    }
}
